package com.lh.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.lh.commonutils.R;
import com.lh.feign.EduVideoFeign;
import com.lh.service.VodService;
import com.lh.util.ConstanVodUtil;
import com.lh.util.InitVodClient;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

/**
 * @author YMS
 * @date 2021/6/8
 */
@Service
public class VodServiceImpl implements VodService {



    @Resource
    private EduVideoFeign eduVideoFeign;

    /**
     * 上传视频到阿里云
     *
     * @param file
     * @return
     */
    @Override
    public R uploadALYVideo(MultipartFile file) {

        String videoSourceId=null;
        try {
            String fileName=file.getOriginalFilename();
            String title=fileName.substring(0,fileName.lastIndexOf("."));
            InputStream inputStream = file.getInputStream();
            UploadStreamRequest request = new UploadStreamRequest(ConstanVodUtil.ACCESS_KEY_ID, ConstanVodUtil.ACCESS_KEY_SECRET, title, fileName, inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);

            if (response.isSuccess()) {
                videoSourceId=response.getVideoId();
            } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
                System.out.print("VideoId=" + response.getVideoId() + "\n");
                System.out.print("ErrorCode=" + response.getCode() + "\n");
                System.out.print("ErrorMessage=" + response.getMessage() + "\n");
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

        return R.ok().data("videoSourceId",videoSourceId);
    }

    /**
     * @param videoSourceId
     * @return
     */
    @Override
    public R removeALYVideo(String videoSourceId) {
        try {
            eduVideoFeign.updateVideoInfo(videoSourceId);
            System.out.println("+++++++++++++++++++++++");
            DefaultAcsClient client = InitVodClient.initVodClient(ConstanVodUtil.ACCESS_KEY_ID, ConstanVodUtil.ACCESS_KEY_SECRET);
            //创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(videoSourceId);
            client.getAcsResponse(request);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * 根据阿里云视频id删除阿里云上的视频
     *
     * @param videoIdList
     * @return
     */
    @Override
    public R deleteBatch(List videoIdList) {


        try {
            DefaultAcsClient client = InitVodClient.initVodClient(ConstanVodUtil.ACCESS_KEY_ID, ConstanVodUtil.ACCESS_KEY_SECRET);
            //创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds( StringUtils.join(videoIdList.toArray(),","));
            client.getAcsResponse(request);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }
}
